﻿using System;
using System.Collections.Generic;
using System.Data;
using eTMS.Controllers.KhuNhaService;
using eTMS.Controllers.PhongHocService;

namespace eTMS.Controllers{
public class KhuNhaController : KhuNhaControllerBase{
	public KhuNhaController()
		: base()
	{
	}
    private DataTable _tblChiTietRemoved;
    private DataTable _tblChiTiet;
    public DataTable TableChiTiet
    {
        get
        {
            return _tblChiTiet;
        }
    }
    public void LoadChiTiet()
    {
        LoadChiTiet(KhuNhaObject.KhuNha_ID);
    }
    private void LoadChiTiet(int CT_ID)
    {
        PhongHocService.PhongHocServiceClient w=new PhongHocServiceClient();
        _tblChiTiet = w.SelectAllDSFilter("KhuNha_ID=" + CT_ID.ToString()).Tables[0];
        _tblChiTietRemoved = _tblChiTiet.Clone();
    }
    public void AddNewChiTiet(string MaPhong,int Tang,int SucChua)
    {
        if (_tblChiTiet == null)
        {
            LoadChiTiet(0);
        }
        PhongHocEntity phongHocEntity=new PhongHocEntity();
        DataRow row = _tblChiTiet.NewRow();
        row["ThuTu"] = _tblChiTiet.Rows.Count + 1;
        row["MaPhong"] = MaPhong;
        row["Tang"] = Tang;
        row["SucChua"] = SucChua;
        _tblChiTiet.Rows.Add(row);
    }
    public void RemoveChiTiet(List<DataRow> list)
    {
        if (_tblChiTietRemoved == null) _tblChiTietRemoved = _tblChiTiet.Clone();
        foreach (DataRow row in list)
        {
            _tblChiTietRemoved.ImportRow(row);
            _tblChiTiet.Rows.Remove(row);
        }
    }
    public void SaveDetail()
    {
        PhongHocController phongHocController=new PhongHocController();
        foreach (DataRow row in _tblChiTietRemoved.Rows)
        {
            int ID = ConvertInt(row["PhongHoc_ID"]);
            if (ID > 0)
                phongHocController.DeleteByID(ID);
        }
        foreach (DataRow row in _tblChiTiet.Rows)
        {
            phongHocController.PhongHocObject = phongHocController.MapDataRowToObject(row);
            phongHocController.PhongHocObject.KhuNha_ID = this.KhuNhaObject.KhuNha_ID;
            if (phongHocController.PhongHocObject.PhongHoc_ID > 0)
                phongHocController.Update();
            else
                phongHocController.AddNew();
        }
    }
    public static List<KhuNhaEntity> FindByTrungTamDaoTao_ID(int TrungTamDaoTao_ID)
    {
        return ListKhuNhaStatic.FindAll(delegate(KhuNhaEntity oFind) { return oFind.TrungTamDaoTao_ID.Equals(TrungTamDaoTao_ID); });
    }
}
}
